﻿using System;
using System.Web.UI;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Payment.Methods.ChronoPay;
using NopSolutions.NopCommerce.BusinessLogic.Model;

namespace NopSolutions.NopCommerce.Web
{
    public partial class ChronoPayIPNHandler : BaseNopFrontendPage
    {
        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(ChronoPayIPNHandler_Load);
        }

        void ChronoPayIPNHandler_Load(object sender, EventArgs e) {
            CommonHelper.SetResponseNoCache(Response);

            if (!Page.IsPostBack) {
                if (HostedPaymentHelper.ValidateResponseSign(Request.Form)) {
                    int orderId = 0;
                    if (Int32.TryParse(Request.Form["cs1"], out orderId)) {
                        Order order = this.OrderService.GetOrderById(orderId);
                        if (order != null && this.OrderService.CanMarkOrderAsPaid(order)) {
                            this.OrderService.MarkOrderAsPaid(order.OrderId);
                        }
                    }
                }
            }
        }

        public override bool AllowGuestNavigation {
            get {
                return true;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this page is tracked by 'Online Customers' module
        /// </summary>
        public override bool TrackedByOnlineCustomersModule {
            get {
                return false;
            }
        }
    }
}
